using System.IO;
using System.Xml.Serialization;

namespace DataAccessLayer
{
    /// <summary>
    /// Handles serialize and deserialize of objects
    /// </summary>
    public static class ObjectSerializer
    {
        /// <summary>
        /// Serializes the specified data and saves it to the given location.
        /// </summary>
        /// <typeparam name="T">The type of data to serialize,</typeparam>
        /// <param name="data">The data to serialize.</param>
        /// <param name="filePathAndName">The file name and path.</param>
        public static void Serialize<T>(T data, string filePathAndName)
        {
            using (Stream strm = new FileStream(filePathAndName, FileMode.Create))
            {
                XmlSerializer serializer = new XmlSerializer(typeof (T));
                serializer.Serialize(strm, data);
            }
        }

        /// <summary>
        /// Deserializes the specified data at the given location.
        /// </summary>
        /// <typeparam name="T">The type of data to deserialize,</typeparam>
        /// <param name="filePathAndName">The file name and path.</param>
        /// <returns>The data of the type given with the data found.</returns>
        public static T Deserialize<T>(string filePathAndName)
        {
            using (Stream strm = new FileStream(filePathAndName, FileMode.Open))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                return (T)serializer.Deserialize(strm);
            }
        }
    }
}